package com.kye.express.business.welfare_integral;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.welfare_integral.viewbinder.IntegralItemViewBinder;
import com.kye.express.databinding.ActivityIntegralItemListBinding;
import com.kye.express.view.ToastManager;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.model.bean.integral.IntegralItemListResultDataBean;
import com.kye.model.bean.integral.IntegralItemType;
import com.kye.model.bean.integral.request.IntegralItemDataBean;
import com.kye.model.bean.user.User;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import net.yslibrary.android.keyboardvisibilityevent.util.UIUtil;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 25/01/2018
 * @updated : 25/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class IntegralItemListActivity extends BaseToolbarActivity implements IntegralItemListConstract.IntegralItemListView {

    private static final String TAG_USER = "TAG_USER";
    private static final String TAG_INTEGRAL_TIME = "TAG_INTEGRAL_TIME";
    private static final String TAG_INTEGRAL_TYPE = "TAG_INTEGRAL_TYPE";
    private ActivityIntegralItemListBinding binding;
    private IntegralItemListConstract.IntegralItemListPresenter presenter;
    private String integralTime;
    private IntegralItemType integralItemType;
    private User userInfo;
    private MultiTypeAdapter adapter;

    public static void startActivity(Context context, String integralTime, IntegralItemType integralItemType) {
        Intent intent = new Intent(context, IntegralItemListActivity.class);
        intent.putExtra(TAG_INTEGRAL_TIME, integralTime);
        intent.putExtra(TAG_INTEGRAL_TYPE, integralItemType);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            integralTime = getIntent().getStringExtra(TAG_INTEGRAL_TIME);
            integralItemType = (IntegralItemType) getIntent().getSerializableExtra(TAG_INTEGRAL_TYPE);
        } else {
            integralTime = savedInstanceState.getString(TAG_INTEGRAL_TIME);
            integralItemType = (IntegralItemType) savedInstanceState.getSerializable(TAG_INTEGRAL_TYPE);
            userInfo = (User) savedInstanceState.getSerializable(TAG_USER);
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_integral_item_list);
        presenter = new IntegralItemListConstract.IntegralItemListPresenter();
        presenter.onCreate(savedInstanceState);
        setUpToolbar(binding.toolbar);
        binding.toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        presenter.onSaveInstanceState(outState);
        outState.putString(TAG_INTEGRAL_TIME, integralTime);
        outState.putSerializable(TAG_INTEGRAL_TYPE, integralItemType);
        outState.putSerializable(TAG_USER, userInfo);
        super.onSaveInstanceState(outState);
    }

    private void initViews() {
        adapter = new MultiTypeAdapter();
        adapter.register(IntegralItemDataBean.class, new IntegralItemViewBinder());
        binding.integralList.setLayoutManager(new LinearLayoutManager(this));
        HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(this)
                .color(getResources().getColor(R.color.window_background))
                .size((int) UIUtil.convertDpToPx(this, 0.5f)).build();
        binding.integralList.addItemDecoration(itemDecoration);
        binding.integralList.setAdapter(adapter);
    }

    private void initEvents() {
        RxView.clicks(binding.llErrorNet.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(binding.llErrorServer.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(binding.llEmpty.btEmptyRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        binding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                presenter.refreshInit(userInfo, integralTime, integralItemType);
            }
        });
    }

    private void initDatas() {
        presenter.bind(this);
    }

    @Override
    public void onUser(User user) {
        userInfo = user;
        binding.statusLayout.showLoadingView();
    }

    @Override
    public void refreshInitSuccess(IntegralItemListResultDataBean integralItemListResultDataBean) {
        if (integralItemListResultDataBean.getDatas() == null || integralItemListResultDataBean.getDatas().size() <= 0) {
            binding.statusLayout.showEmptyView();
            return;
        }
        binding.refreshLayout.setEnableLoadmore(integralItemListResultDataBean.getDatas().size() >= presenter.getPageInfo().getPageSize());
        binding.statusLayout.showContentView();
        adapter.setItems(integralItemListResultDataBean.getDatas());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void refreshInitNetFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.statusLayout.showErrorNetworkView();
    }

    @Override
    public void refreshInitServerFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.statusLayout.showErrorServerView();
    }

    @Override
    public void refreshSuccess(IntegralItemListResultDataBean integralItemListResultDataBean) {
        if (integralItemListResultDataBean.getDatas() == null || integralItemListResultDataBean.getDatas().size() <= 0) {
            binding.statusLayout.showEmptyView();
            return;
        }
        binding.refreshLayout.setEnableLoadmore(integralItemListResultDataBean.getDatas().size() >= presenter.getPageInfo().getPageSize());
        binding.refreshLayout.finishRefresh(true);
        adapter.setItems(integralItemListResultDataBean.getDatas());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void refreshNetFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.refreshLayout.finishRefresh(false);
    }

    @Override
    public void refreshServerFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.refreshLayout.finishRefresh(false);
    }

    @Override
    public void loadmoreSuccess(IntegralItemListResultDataBean integralItemListResultDataBean) {
        binding.refreshLayout.setEnableLoadmore(integralItemListResultDataBean.getDatas().size() >= presenter.getPageInfo().getPageSize());
        binding.refreshLayout.finishLoadmore(true);
        List<IntegralItemDataBean> datas = (List<IntegralItemDataBean>) adapter.getItems();
        int startPosition = datas.size();
        datas.addAll(integralItemListResultDataBean.getDatas());
        adapter.setItems(datas);
        adapter.notifyItemRangeInserted(startPosition, datas.size());
    }

    @Override
    public void loadmoreNetFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.refreshLayout.finishLoadmore(false);
    }

    @Override
    public void loadmoreServerFailed(String msg) {
        ToastManager.showCenterForShort(msg);
        binding.refreshLayout.finishLoadmore(false);
    }

    @Override
    public void enableLoadmore() {
        binding.refreshLayout.setEnableLoadmore(true);
    }

    @Override
    public void disableLoadmore() {
        binding.refreshLayout.setEnableLoadmore(false);
    }
}
